<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use App\Http\Requests\Api\RoleRequest;
use Spatie\Permission\Models\Permission;
use App\Transformers\RoleTransformer;

class RolesController extends Controller
{
    public function index(Request $request, Role $role)
    {
        $this->authorize('update', $role);

        $per_page = $request->input('per_page', 20);
        $query = $role->query();

        $roles = $query->->recent()->paginate($per_page);

        return $this->response->paginator($roles, new RoleTransformer());
    }

    public function userIndex(Request $request, Role $role)
    {

        $per_page = $request->input('per_page', 20);
        // $query = $role->query();

        $roles = $this->user->roles()->recent()->paginate($per_page);

        return $this->response->paginator($roles, new RoleTransformer());
    }

    public function addPermissons(Request $request, Role $role)
    {
        $this->authorize('update', $role);

        $role->givePermissionTo($request->permissions);

        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'添加权限成功',
        ])->setStatusCode(200);
    }

    public function removePermissons(Request $request, Role $role)
    {
        $this->authorize('update', $role);

        $role->givePermissionTo($request->permission);

        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'删除权限成功',
        ])->setStatusCode(200);
    }

    public function show(Role $role)
    {
        $this->authorize('update', $role);

        return $this->response->item($role, new RoleTransformer());
    }

    public function store(RoleRequest $request, Role $role)
    {

        $this->authorize('update', $role);

        $role->fill($request->all());
        $role->save();

        return $this->response->item($role, new RoleTransformer())
            ->setStatusCode(201);
    }

    public function update(RoleRequest $request, Role $role)
    {
        $this->authorize('update', $role);

        $role->update($request->all());

        return $this->response->item($role, new RoleTransformer());
    }

    public function destroy(Role $role)
    {
        $this->authorize('destroy', $role);

        $role->delete();

        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'删除成功',
        ])->setStatusCode(200);
    }

    public function assignRole(Request $request, User $user)
    {
        $this->authorize('update', $role);

        $user->assignRole($request->role);

        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'用户指定角色成功',
        ])->setStatusCode(200);
    }
}
